package com.sskj.mine.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jkb.vcedittext.VerificationCodeEditText;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.enums.CodeTypeEnum;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.GoogleCode;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.VerifyGoogleActivityPresenter;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;


public class VerifyGoogleActivity extends BaseActivity<VerifyGoogleActivityPresenter> {
    @BindView(R2.id.iv_qr_code)
    ImageView mIvQrCode;
    @BindView(R2.id.tv_code)
    TextView mTvCode;
    @BindView(R2.id.tv_code_copy)
    TextView mTvCodeCopy;
    @BindView(R2.id.google_a_layout)
    LinearLayout mGoogleALayout;
    @BindView(R2.id.etVerificationCode)
    VerificationCodeEditText mEtVerificationCode;
    @BindView(R2.id.google_b_layout)
    LinearLayout mGoogleBLayout;
    @BindView(R2.id.btn_next)
    Button mBtnNext;
    @BindView(R2.id.left_tv)
    TextView left_tv;
    private BottomSheetDialog sellDialog;
    private TextView mTvCount;
    private GoogleCode obj;
    /**
     * 判断用户是否验证成功
     */
    private boolean isSuccess;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BottomSheetDialog sheetDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_verify_google;
    }

    @Override
    public VerifyGoogleActivityPresenter getPresenter() {
        return new VerifyGoogleActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mineverifyGoogleActivity1));
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        mPresenter.requestData();

        ClickUtil.click(mTvCodeCopy, () -> {
            String trim = mTvCode.getText().toString().trim();
            CopyUtil.copy(trim);
        });
        ClickUtil.click(mBtnNext, () -> {
            showSmsVerifyDialog();
        });
    }


    /**
     * 显示手机验证码Dialog
     */
    private void showSmsVerifyDialog() {
        sheetDialog = BottomSheetUtil.showCheck(this, App.INSTANCE.getString(R.string.mineverifyGoogleActivity2), false, true, true, CodeTypeEnum.STATUS_5, (pwd, googleCode, smsCode) -> {
            sheetDialog.dismiss();
            mPresenter.submit(googleCode, smsCode, obj.getSecret(), obj.getQrcode());
        });
        sheetDialog.show();

    }



    public void onGoogleCodeSuccess(GoogleCode obj) {
        this.obj = obj;
        mTvCode.setText(obj.getSecret());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.skipMemoryCache(true);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(getApplicationContext()).load(obj.getQrcode()).apply(requestOptions).into(mIvQrCode);

    }

    public void onBindSuccess() {
        finish();
        isSuccess = true;
    }

}
